<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {height:34px;line-height:34px;padding:0 8px;}
</style>
<body>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="add">添加子菜单</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
	//树形表格渲染参数
	var tree_table_render_params = {}
    layui.use(['table', 'treetable','miniPage'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
		var miniPage = layui.miniPage;
		var reqUrl = '/admin/rbac/menu'
		
        // 表格参数设置
		tree_table_render_params = {
			treeColIndex: 1,
			treeSpid: -1,
			treeIdName: 'id',
			treePidName: 'ParentId',
			elem: '#munu-table',
			url: reqUrl,
			page: false,
			toolbar: ['<p>',
				'<button lay-event="add_root" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
				'</p>'].join(''),
			method:'post', //这里可以写post请求,默认get	
			cols: [[
				{type: 'numbers'},
				{field: 'AuthorityName', minWidth: 200, title: '权限名称'},
				{field: 'Authority', title: '权限标识'},
				{field: 'MenuUrl', title: '菜单url'},
				{field: 'Status', width: 80, align: 'center', title: '状态',templet: function (d) {return d.Status ==0 ? '显示':"隐藏";}},
				{field: 'OrderNumber', width: 80, align: 'center', title: '排序号'},
				{
					field: 'IsMenu', width: 80, align: 'center', templet: function (d) {
						if (d.IsMenu == 1) {
							return '<span class="layui-badge layui-bg-gray">按钮</span>';
						}
						if (d.ParentId == -1) {
							return '<span class="layui-badge layui-bg-blue">目录</span>';
						} else {
							return '<span class="layui-badge-rim">菜单</span>';
						}
					}, title: '类型'
				},
				{templet: '#auth-state', width: 200, align: 'center', title: '操作'}
			]],
			done: function () {
				layer.closeAll('loading');
			}
		}
        
		// 渲染表格
		treetable.render(tree_table_render_params);
		
        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                //layer.msg('删除' + data.id);
				layer.confirm('请确保菜单未分配任何权限,菜单删除时会同步删除子菜单,您确定要删除吗？', function (index) {
					//console.log(obj.data.id)
					var del_url = "/admin/rbac/menuDel"
					var id = data.id
					$.ajax({
						url: del_url,
						type: 'POST',
						data: {id:id},//_method:"DELETE" 发送 DELETE 请求
						dataType: "json",
						success: function(result) {
							// Do something with the result
							if(result.code == 0){
								//_obj.del();
								tree_table_render_params.url = reqUrl //要刷新的数据源url
								treetable.reload(tree_table_render_params);
							}else{
								layer.msg(result.msg)
							}
							
							console.log(result)
						}
					});
                   
                    layer.close(index);
                });
				
            } else if (layEvent === 'edit') {
                //layer.msg('修改' + data.id);
				var id = data.id
				var content = miniPage.getHrefContent('/admin/rbac/menuEdit?id='+id);
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '编辑菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
               
            } else if (layEvent === "add") {
				var parent_id = data.id
				//layer.msg("添加子菜单");
				var content = miniPage.getHrefContent('/admin/rbac/menuAdd?parent_id='+parent_id);
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加子菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                
			} 
        });
		
		/**
         * toolbar事件监听
         */
        table.on('toolbar(munu-table)', function (obj) {
            if (obj.event === 'add_root') {   // 监听添加操作
                var content = miniPage.getHrefContent('/admin/rbac/menuAdd');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                
            }
		});
    });
	
</script>
</body>
</html>